package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.LeaseTermService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    ApartmentInfoService apartmentInfoService;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    PaymentTypeMapper paymentTypeMapper;
    @Autowired
    LeaseTermMapper leaseTermMapper;
    @Autowired
    AttrValueMapper attrValueMapper;
    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    FeeValueMapper feeValueMapper;
    @Autowired
    LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    BrowsingHistoryService browsingHistoryService;

    @Override
    public IPage<RoomItemVo> getPageItem(Page<RoomItemVo> roomIPageView, RoomQueryVo queryVo) {
        return roomInfoMapper.getPageItem(roomIPageView,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();

        //查询房间信息
        RoomInfo roomInfo = getById(id);

        //所属公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.getApartInfo(roomInfo.getApartmentId());
        //房间图片
        List<GraphVo> graphVoList = graphInfoMapper.getGraphInfo(ItemType.ROOM,id);
        //支付类型
        List<PaymentType> paymentTypeList = paymentTypeMapper.getPaymentTypeInfo(id);
        //租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.getLeaseTermInfo(id);
        //房间属性
        List<AttrValueVo> attrValueVoList = attrValueMapper.getAttrValueInfo(id);
        //房间标签
        List<LabelInfo> labelInfoList = labelInfoMapper.getLabelInfo(id);
        //房间配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getFacilityInfo(id);
        //公寓杂费
        List<FeeValueVo> feeValueVoList = feeValueMapper.getFeeValueInfo(roomInfo.getApartmentId());
        //是否入住
        Long count = leaseAgreementMapper.selectCount(new LambdaQueryWrapper<LeaseAgreement>().eq(LeaseAgreement::getRoomId, id).in(LeaseAgreement::getStatus, 2, 5));


        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setIsCheckIn(count>0);
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);

        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        //保存浏览历史
        browsingHistoryService.saveHistory(LoginUserContext.getUser().getUserId(), id);

        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> getRoomDetail(IPage<RoomItemVo> roomItemIPage, Long id) {
        return roomInfoMapper.getRoomDetail(roomItemIPage,id);
    }
}




